var NEIGHBORHOODS = new Array();
NEIGHBORHOODS.push( new Neighborhood("paris1","http://openstreetmapdemo.chaimae.cloudbees.net/kml/paris1.kml"));
NEIGHBORHOODS.push( new Neighborhood("paris2","http://openstreetmapdemo.chaimae.cloudbees.net/kml/paris2.kml"));
NEIGHBORHOODS.push( new Neighborhood("paris3","http://openstreetmapdemo.chaimae.cloudbees.net/kml/paris3.kml"));

var cloudmade = new CM.Tiles.CloudMade.Web({key: '0c3369866dc64ad3b30e0b190544a408'});
var MAP;

shapes = [];
markers = [];
overlays = [];

// Call this function when the page has been loaded
function initialize(latitude, longitude) {
	MAP = new CM.Map('map_canvas', cloudmade);
	MAP.setCenter(new CM.LatLng(48.833, 2.333), 11);
	
	var topRight = new CM.ControlPosition(CM.TOP_RIGHT, new CM.Size(50, 20));
	//MAP.addControl(new CM.SmallMapControl(), topRight);
	MAP.addControl(new CM.LargeMapControl());
	//MAP.addControl(new CM.ScaleControl());
	MAP.addControl(new CM.OverviewMapControl());

	MAP.setCenter(new CM.LatLng(latitude, longitude), 11);

	clearOverlays();
}

function clearOverlays() {
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		NEIGHBORHOODS[i].drawit = false;
    }
	
	if (overlays) {
		for (i in overlays) {
			MAP.removeOverlay(overlays[i]);
		}
	}
	
	markers = [];
	shapes = [];
}

function resetMap(latitude, longitude) {
	MAP.setCenter(new CM.LatLng(latitude, longitude), 11);

	clearOverlays();
}

function showArrondissement(longitude, latitude, arrondissement) {
	MAP.setCenter(new CM.LatLng(latitude, longitude), 11);	
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		if (arrondissement == NEIGHBORHOODS[i].id) {
			NEIGHBORHOODS[i].drawit = true;
			break;
		}
    }

    addOverlays();
}

function addOverlays() {
	
	// ADD OVERLAY
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		if (NEIGHBORHOODS[i].drawit) {
			if (contains(shapes,NEIGHBORHOODS[i].id)) {
				continue;
			}
			var geoxml = new CM.GeoXml(NEIGHBORHOODS[i].kml);

			CM.Event.addListener(geoxml, 'load', function() {
			    MAP.zoomToBounds(geoxml.getDefaultBounds());
			    MAP.addOverlay(geoxml);
			});
			
			shapes.push(NEIGHBORHOODS[i].id);
		}
	}
}


function addStaticMarker(id, lat, lng) {
	if (!contains(markers,id)) {
		// static marker
		var myMarkerLatLng = new CM.LatLng(48.84,2.333);
		var myMarker = new CM.Marker(myMarkerLatLng, {
			title: "This is the Village of Naurod"
		});
		    
		CM.Event.addListener(myMarker, 'click', function() {
			myMarker.openInfoWindow("The GLatLng value is: " + myMarker.getLatLng() + " at zoom level " + MAP.getZoom());
		});
		
		MAP.setCenter(myMarkerLatLng, 11);
		MAP.addOverlay(myMarker);
		overlays.push(myMarker);
	}
		
	markers.push(id);
}

function addDraggableMarker(id, lat, lng) {
	if (!contains(markers,id)) {
		// draggable marker
		var myMarkerLatLng = new CM.LatLng(48.863037,2.320769);
		var myMarker = new CM.Marker(myMarkerLatLng, {
			title: "The first complete city in OpenStreetMap",
			draggable: true
		});
		   
		
		MAP.setCenter(myMarkerLatLng, 11);
		MAP.addOverlay(myMarker);
		
		CM.Event.addListener(myMarker, 'click', function() {
			myMarker.openInfoWindow("The GLatLng value is: " + myMarker.getLatLng() + " at zoom level " + MAP.getZoom());
		});
		
		CM.Event.addListener(myMarker, 'dragend', function() {
			myMarker.openInfoWindow("The GLatLng value is: " + myMarker.getLatLng() + " at zoom level " + MAP.getZoom());
		});
		
		overlays.push(myMarker);
	}
	
	markers.push(id);
}

function Neighborhood(id, kml) {
    this.id = id;
    this.drawit = false;
    this.kml = kml;
}

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}